package com.graduation.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.model.entity.ActivityPart.Gift;
import com.graduation.springbootinit.model.entity.ActivityPart.ExchangeRecord;
import com.graduation.springbootinit.model.entity.User;
import com.graduation.springbootinit.model.request.GiftExchangeRequest;
import com.graduation.springbootinit.service.GiftService;
import com.graduation.springbootinit.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/gift")
public class ActivityGiftController {

    @Resource
    private GiftService giftService;

    @Resource
    private UserService userService;

    // 管理员添加礼品
    @PostMapping("/add")
    public BaseResponse<Boolean> addGift(@RequestBody Gift gift, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        // TODO: 验证用户是否是管理员
        giftService.createGift(gift);
        return ResultUtils.success(true);
    }

    // 获取礼品列表
    @GetMapping("/list")
    public BaseResponse<Page<Gift>> listGifts(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResultUtils.success(giftService.listGifts(pageNum, pageSize));
    }

    // 兑换礼品
    @PostMapping("/exchange")
    public BaseResponse<Boolean> exchangeGift(@RequestBody GiftExchangeRequest request, HttpServletRequest httpRequest) {
        User user = userService.getLoginUser(httpRequest);
        giftService.exchangeGift(user.getId(), request.getGiftId(), request.getAddressId());
        return ResultUtils.success(true);
    }

    // 获取用户的兑换记录
    @GetMapping("/exchange/records")
    public BaseResponse<Page<ExchangeRecord>> getExchangeRecords(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return ResultUtils.success(giftService.getUserExchangeRecords(user.getId(), pageNum, pageSize));
    }

    // 管理员更新兑换记录状态
    @PostMapping("/exchange/status")
    public BaseResponse<Boolean> updateExchangeStatus(@RequestBody ExchangeRecord record, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        // TODO: 验证用户是否是管理员
        giftService.updateExchangeStatus(record.getId(), record.getStatus());
        return ResultUtils.success(true);
    }
} 